<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/5/14
 * Time: 17:12
 */
namespace Admin\Controller;

class DepartController extends BaseController{

    public function index(){
        $Depart = D('Depart');
        $DepartList = $Depart->getAllDepartWhere();
        $this->assign('DepartList',$DepartList);

        $this->display();
    }

    public function del(){
        $id = $_REQUEST['id'];
        if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        $Depart = D ('Depart');
        $flag = $Depart->getDelDepart($id);
        if ($flag !== false)
            $this->out('RESULT_OK','',__CONTROLLER__);
        $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
    }

    public function add(){
        if(!IS_POST){
            $Depart = D('Depart');
            $DepartList = $Depart->getAllDepartWhere();
            $this->ajaxReturn($DepartList);
        }else{
            $this->isPost();//验证是否是post方式提交数据
            $data['name'] = $_POST['depart_name'];
            $data['f_id'] = $_POST['depart_id'];


            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
            $Depart = D('Depart');
            $flag = $Depart->addDepart($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }
    }

    public function edit(){
        if(!IS_POST){
            $data['id'] = $_GET['id'];
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->error(\Api\Libs\ExplainRespCodeUtil::explain($flag));
            $Depart = D('Depart');
            $D = $Depart->getDepartById($data);

            if (empty($D)) $this->error('不存在该记录');
            $XyList = $Depart->getAllXyname();
            $this->assign('XyList',$XyList);
            $this->assign('D',$D);
            $this->display();
        }else{
            $this->isPost();//验证是否是post方式提交数据
            $data['id'] = $_POST['id'];
            $data['name'] = $_POST['depart_name'];
            $data['f_id'] = $_POST['depart_id'];


            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
            $Depart = D('Depart');
            $flag = $Depart->editDepart($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }
    }


    public function add_xy(){
        if (!IS_POST) {
            $this->display();
        } else {
            $data['name'] = $_POST['name'];
            $data['f_id'] = 0;
            $data['grade'] = 0;
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
            $Depart = D ('Depart');
            $flag = $Depart->addDepart($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }
    }

    public function edit_xy(){
        if(!IS_POST){
            $data['id'] = $_GET['id'];
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->error(\Api\Libs\ExplainRespCodeUtil::explain($flag));
            $Depart = D('Depart');
            $D = $Depart->getDepartById($data);
            if (empty($D)) $this->error('不存在该记录');
            $this->assign('D',$D);
            $this->display();
        }else{
            $this->isPost();//验证是否是post方式提交数据
            $data['id'] = $_POST['id'];
            $data['name'] = $_POST['name'];
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
            $Depart = D('Depart');
            $flag = $Depart->editDepart($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }
    }

    /**
     *添加专业
     */
    public function add_zy(){
        if(!IS_POST){
            $Depart = D('Depart');
            $DepartList = $Depart->getAllXyName();
            $this->assign('DepartList',$DepartList);
            $this->display();

        }else{
            $data['name'] = $_POST['name'];
            $data['f_id'] = $_POST['id'];
            $data['grade'] = 1;
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
            $Depart = D ('Depart');
            $flag = $Depart->addDepart($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }

    }


    public function edit_zy(){
        if(!IS_POST){
            $data['id'] = $_GET['id'];
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->error(\Api\Libs\ExplainRespCodeUtil::explain($flag));
            $Depart = D('Depart');
            $D = $Depart->getDepartById($data);
            if (empty($D)) $this->error('不存在该记录');
            $DepartList = $Depart->getAllXyName();
            $this->assign('DepartList',$DepartList);
            $this->assign('D',$D);
            $this->display();
        }else{
            $this->isPost();//验证是否是post方式提交数据
            $data['id'] = $_POST['id'];
            $data['f_id']= $_POST['f_id'];
            $data['name'] = $_POST['name'];
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
            $Depart = D('Depart');
            $flag = $Depart->editDepart($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }
    }
    /**
     * 添加班级
     */
    public function add_bj(){
        if(!IS_POST){
            $Depart = D('Depart');
            $DepartList = $Depart->getAllXyName();
            $this->assign('DepartList',$DepartList);
            $this->display();

        }else{
            $x_id = $_POST['x_id'];//学院id
            $data['f_id'] = $_POST['z_id'];//专业id
            $data['name'] = $_POST['bj_name'];//班级名称
            $data['grade'] = 2;
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
            $Depart = D ('Depart');
            $yz = $Depart->getYz($data,$x_id);//验证专业是不是属于学院id;
            if ($yz !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($yz));
            $flag = $Depart->addDepart($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }
    }


    public function edit_bj(){
        if(!IS_POST){
            $data['id'] = $_GET['id'];
            $data['f_id'] = $_GET['f_id'];
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->error(\Api\Libs\ExplainRespCodeUtil::explain($flag));
            $Depart = D('Depart');
            $D = $Depart->getDepartById($data);
            if (empty($D)) $this->error('不存在该记录');
            $Zy = $Depart->getZyById($data['f_id']);
            $XyList = $Depart->getAllXyname();
            $ZyList = $Depart->getAllXyById($Zy['f_id']);
            $this->assign('D',$D);

            $this->assign('Zy',$Zy);
            $this->assign('XyList',$XyList);
            $this->assign('ZyList',$ZyList);

            $this->display();
        }else{
            $this->isPost();//验证是否是post方式提交数据
            $x_id = $_POST['x_id'];//学院id
            $data['id'] = $_POST['b_id'];
            $data['f_id'] = $_POST['z_id'];//专业id
            $data['name'] = $_POST['bj_name'];

            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
            $Depart = D('Depart');
            $yz = $Depart->getYz($data,$x_id);//验证专业是不是属于学院id;
            if ($flag !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
            $flag = $Depart->editDepart($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }
    }

    public function getZY(){

        if(empty($_GET['x_id'])){
            $data['status']  = 1;
            $this->ajaxReturn(['status'=>0,'msg'=>'error: x_id is not found!']);
            die();
        }
        $x_id = $_GET['x_id'];
        $Depart = D('Depart');
        $res = $Depart->getAllXyById($x_id);

        $this->ajaxReturn(['status'=>1,'data'=>$res]);
    }


}